package com.erp.system.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.erp.common.controller.BaseController;
import com.erp.common.utils.*;
import com.erp.oa.domain.NotifyDO;
import com.erp.oa.service.NotifyService;
import com.erp.system.domain.UserDO;
import com.erp.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.system.domain.TravelDO;
import com.erp.system.service.TravelService;

import javax.servlet.http.HttpServletResponse;

/**
 * 出差管理
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-11-18 22:32:48
 */
 
@Controller
@RequestMapping("/system/travel")
public class TravelController extends BaseController {
	@Autowired
	private TravelService travelService;
	@Autowired
	private UserService userService;
	@Autowired
	private NotifyService notifyService;
	
	@GetMapping()
	String Travel(){
	    return "system/travel/travel";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		//查询当前登录人的角色信息是否有部门经理和总经理
		int mgrcount = userService.isManagerOrCeo(getUserId());
		if(mgrcount <=0 ){
			params.put("createIdUser",getUserId());
		}else{
			if(getUser().getDeptId() != 16){
				params.put("createIdUserss",getUserId());
			}
		}
        Query query = new Query(params);
		List<TravelDO> travelList = travelService.list(query);
		int total = travelService.count(query);
		PageUtils pageUtils = new PageUtils(travelList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "system/travel/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		TravelDO travel = travelService.get(id);
		model.addAttribute("travel", travel);
	    return "system/travel/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( TravelDO travel){
		travel.setCreateIdUser(getUserId());
		travel.setCreateNameUser(getUser().getName());
		travel.setGmtCreate(new Date());
		travel.setTravelUserid(getUserId());
		travel.setTravelName(getUser().getName());
		travel.setStatus(1);
		if(travel.getDepetLeaderid() !=null){
			UserDO userDO = userService.get(travel.getDepetLeaderid());
			travel.setDepetLeadername(userDO.getName());
		}
		if(travelService.save(travel)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( TravelDO travel){
		travelService.update(travel);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(travelService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}

	@PostMapping( "/okconfirm")
	@ResponseBody
	public R okconfirm(Long id, Integer status){
		if(travelService.okconfirm(id,status)>0){
			TravelDO travel = travelService.get(id);
			//发通知和邮件通知采购和总经理
			NotifyDO notify = new NotifyDO();
			notify.setType("5");
			notify.setTitle("出差申请");
			List<Long> deprids = new ArrayList<>();
			deprids.add(16L);
			List<UserDO> userDOList =userService.getUsersByDeptIds(deprids);
			List<Long> userId = userDOList.stream().map(UserDO::getUserId).collect(Collectors.toList());
			if(status == 2){
				notify.setContent(travel.getTravelName()+"申请到["+travel.getTravelDestination()+"]出差 /n"+
						"出差时间为:["+ DateUtils.format(travel.getTravelStarttime())+"-"+DateUtils.format(travel.getTravelEndtime())+"]");
				userId.add(travel.getDepetLeaderid());
			}else if(status == 3){
				notify.setContent(travel.getTravelName()+"申请到["+travel.getTravelDestination()+"]出差 /n"+
						"出差时间为:["+ DateUtils.format(travel.getTravelStarttime())+"-"+DateUtils.format(travel.getTravelEndtime())+"]审核通过");
				userId.add(travel.getTravelUserid());
			}else if(status == 4){
				notify.setContent(travel.getTravelName()+"申请到["+travel.getTravelDestination()+"]出差 /n"+
						"出差时间为:["+DateUtils.format(travel.getTravelStarttime())+"-"+DateUtils.format(travel.getTravelEndtime())+"]审核被拒绝");
				userId.add(travel.getTravelUserid());
			}
			Long[] userIds =userIds = userId.toArray( new Long[userId.size()]);
			notify.setUserIds(userIds);
			notify.setStatus("1");
			notify.setCreateBy(getUserId());
			notifyService.save(notify);

			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		travelService.batchRemove(ids);
		return R.ok();
	}


	@GetMapping("/tracelreport")
	String tracelreport(){
		return "system/travel/tracelreport";
	}


	@ResponseBody
	@GetMapping("/tracelreportlist")
	public PageUtils tracelreportlist(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<TravelDO> travelList = travelService.reportlist(query);
		int total = travelService.reportcount(query);
		PageUtils pageUtils = new PageUtils(travelList, total);
		return pageUtils;
	}


	@PostMapping("/exportreportData")
	@ResponseBody
	public void downloadTemplate(TravelDO travelDO, HttpServletResponse response){
		//ProjectDO project
		Map<String, Object> params = new HashMap<>();
		if(travelDO.getStatus() !=null){
			params.put("status",travelDO.getStatus());
		}
		if(travelDO.getTravelStarttime() != null){
			params.put("travelStarttime",travelDO.getTravelStarttime());
		}
		if(travelDO.getTravelEndtime() != null){
			params.put("travelEndtime",travelDO.getTravelEndtime());
		}
		List<TravelDO> travelDOList = travelService.reportlist(params);
		ExcelUtil.exportExcel(travelDOList,"出差统计","出差统计", TravelDO.class,"出差统计.xls",response);
	}


}
